/*
 * 创建日期 2009-3-24
 *
 * 成都天和软件公司
 * 电话：028-85425861
 * 传真：028-85425861-8008
 * 邮编：610041 
 * 版权所有
 */
package  org.fantasy.cpp.web.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import  org.fantasy.cpp.core.pojo.ChartConfig;
import  org.fantasy.cpp.core.pojo.QueryConfig;
import  org.fantasy.cpp.core.pojo.QueryItem;
import  org.fantasy.cpp.core.pojo.QueryPage;
import  org.fantasy.cpp.core.pojo.QueryTemplet;
import  org.fantasy.cpp.core.pojo.TableConfig;
import  org.fantasy.cpp.core.service.ConfigService;
import  org.fantasy.common.db.bean.Row;
import  org.fantasy.common.util.DateUtil;
import  org.fantasy.common.util.HtmlUtil;
import  org.fantasy.common.util.ParamMap;
import  org.fantasy.common.util.RequestUtil;

/**
 * 页面控制
 * 
 * @author: 王文成
 * @version: 1.0
 * @since 2011-3-24
 */
@Controller
public class ConfigController extends AbstractController {

	private static final Log log = LogFactory.getLog(ConfigController.class);

	@Autowired
	private ConfigService configService;

	/**
	 * 查询页面初始化
	 * 
	 * @param pageId
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/page/config/queryPageEdit")
	public String queryPageEdit(Long id, ModelMap model) throws Exception {
		if (id != null) {
			QueryPage bean = configService.getQueryPage(id);
			model.addAttribute("bean", bean);
			model.addAttribute("items", configService.getSelectQueryItemList(id));
			model.addAttribute("itemu", configService.getUnSelectQueryItemList(id));
		} else {
			QueryPage bean = new QueryPage();
			String pageCode = DateUtil.format(new Date(), "yyyyMMddHHmmss");
			bean.setPageCode(pageCode);
			model.addAttribute("bean", bean);
			model.addAttribute("itemu", configService.getQueryItemList());
		}
		return "/page/config/queryPageEdit";
	}

	/**
	 * 查询页面初始化
	 * 
	 * @param pageId
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/page/config/queryPageSave")
	public String queryPageSave(HttpServletRequest request, ModelMap model) throws Exception {
		ParamMap<String, Object> params = RequestUtil.getParamMap(request);
		QueryPage bean = configService.saveQueryPage(params);
		model.addAttribute(ID, bean.getPageId());
		return result(model);
	}

	/**
	 * 查询页面初始化
	 * 
	 * @param pageId
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/page/config/queryItemMain")
	public String queryItemMain(Long pageId, ModelMap model) throws Exception {
		return "/page/config/queryItemMain";
	}

	/**
	 * 查询页面初始化
	 * 
	 * @param pageId
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/page/config/queryItemList")
	public String queryItemList(Long pageId, ModelMap model) throws Exception {
		return "/page/config/queryItemList";
	}

	/**
	 * 查询条件初始化
	 * 
	 * @param pageId
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/page/config/queryItemEdit")
	public String queryItemEdit(Long id, ModelMap model) throws Exception {
		if (id != null) {
			QueryItem bean = configService.getQueryItem(id);
			model.addAttribute("bean", bean);
		} else {
			QueryItem bean = new QueryItem();
			model.addAttribute("bean", bean);
		}
		return "/page/config/queryItemEdit";
	}

	/**
	 * 查询条件保存
	 * 
	 * @param pageId
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/page/config/queryItemSave")
	public String queryItemSave(HttpServletRequest request, ModelMap model) throws Exception {
		ParamMap<String, Object> params = RequestUtil.getParamMap(request);
		QueryItem bean = configService.saveQueryItem(params);
		model.addAttribute(ID, bean.getItemId());
		return result(model);
	}

	/**
	 * 删除查询条件
	 * 
	 * @param id
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/page/config/queryItemDel")
	public String queryItemDel(Long id, ModelMap model) throws Exception {
		configService.removeQueryItem(id);
		return ajax("success", model);
	}

	/**
	 * 查询配置初始化
	 * 
	 * @param pageId
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/page/config/queryConfigEdit")
	public String queryConfigEdit(Long id, ModelMap model) throws Exception {
		if (id != null) {
			QueryConfig bean = configService.getQueryConfig(id);
			model.addAttribute("bean", bean);
		} else {
			QueryConfig bean = new QueryConfig();
			model.addAttribute("bean", bean);
		}
		return "/page/config/queryConfigEdit";
	}

	/**
	 * 查询配置初始化
	 * 
	 * @param pageId
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/page/config/queryConfigTest")
	public String queryConfigTest(Long id, HttpServletRequest request, ModelMap model) throws Exception {
		ParamMap<String, Object> pm = RequestUtil.getParamMap(request);
		List<Row> rows = configService.executeQueryConfig(id, pm);
		return ajax(HtmlUtil.toPageHtml(rows), model);
	}

	/**
	 * 查询配置保存
	 * 
	 * @param pageId
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/page/config/queryConfigSave")
	public String queryConfigSave(HttpServletRequest request, ModelMap model) throws Exception {
		ParamMap<String, Object> params = RequestUtil.getParamMap(request);
		QueryConfig bean = configService.saveQueryConfig(params);
		model.addAttribute(ID, bean.getQueryId());
		return result(model);
	}

	/**
	 * 表格配置初始化
	 * 
	 * @param pageId
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/page/config/tableConfigEdit")
	public String tableConfigEdit(Long id, Long queryId, ModelMap model) throws Exception {
		if (id != null) {
			TableConfig bean = configService.getTableConfig(id);
			model.addAttribute("bean", bean);
		} else {
			TableConfig bean = new TableConfig();
			String code = DateUtil.format(new Date(), "yyyyMMddHHmmss");
			bean.setTableCode(code);
			String tableConfig = configService.guessTableConfig(queryId);
			model.addAttribute("bean", bean);
			model.addAttribute("tableConfig", tableConfig);
		}
		return "/page/config/tableConfigEdit";
	}

	/**
	 * 表格配置保存
	 * 
	 * @param pageId
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/page/config/tableConfigSave")
	public String tableConfigSave(HttpServletRequest request, ModelMap model) throws Exception {
		ParamMap<String, Object> params = RequestUtil.getParamMap(request);
		TableConfig bean = configService.saveTableConfig(params);
		model.addAttribute(ID, bean.getTableId());
		return result(model);
	}

	/**
	 * 图表配置初始化
	 * 
	 * @param pageId
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/page/config/chartConfigEdit")
	public String chartConfigEdit(Long id, Long tableId, ModelMap model) throws Exception {
		if (id != null) {
			ChartConfig bean = configService.getChartConfig(id);
			model.addAttribute("bean", bean);
		} else {
			ChartConfig bean = new ChartConfig();
			String chartConfig = configService.guessChartConfig(tableId);
			model.addAttribute("chartConfig", chartConfig);
			model.addAttribute("bean", bean);
		}
		return "/page/config/chartConfigEdit";
	}

	/**
	 * 图表配置保存
	 * 
	 * @param pageId
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/page/config/chartConfigSave")
	public String chartConfigSave(HttpServletRequest request, ModelMap model) throws Exception {
		ParamMap<String, Object> params = RequestUtil.getParamMap(request);
		ChartConfig bean = configService.saveChartConfig(params);
		model.addAttribute(ID, bean.getChartId());
		return result(model);
	}

	/**
	 * 页面模板编辑
	 * 
	 * @param pageId
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/page/config/queryTempletEdit")
	public String queryTempletEdit(Long id, ModelMap model) throws Exception {
		if (id != null) {
			QueryTemplet bean = configService.getQueryTemplet(id);
			model.addAttribute("bean", bean);
		} else {
			QueryTemplet bean = new QueryTemplet();
			model.addAttribute("bean", bean);
		}
		return "/page/config/queryTempletEdit";
	}

	/**
	 * 页面模板初始化
	 * 
	 * @param pageId
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/page/config/queryTempletSave")
	public String queryTempletSave(HttpServletRequest request, ModelMap model) throws Exception {
		ParamMap<String, Object> params = RequestUtil.getParamMap(request);
		QueryTemplet bean = configService.saveQueryTemplet(params);
		model.addAttribute(ID, bean.getTempletId());
		return result(model);
	}
}